const express = require("express")
const cors = require("cors") 
const mysql = require("mysql")


const app = express();
const db = mysql.createPool({
  host : 'localhost',
  user : 'root',
  password : '123456',
  database : 'study_data',
  port : '3306'
})

app.use(cors())
app.use(express.json())
app.use(express.urlencoded({extended : false}))

app.get("/",(req,res)=>{

  db.query('SELECT * FROM info',(err,resulte)=>{
    console.log(resulte)
  })

  res.send("响应成功~~~")
})

app.get("/add",(req,res)=>{
  // 或者 插入替换对象 ('INSERT INTO info SET ?',{name:'小白',url:'对象地址'})
  // ('INSERT INTO info SET name=?,image=? WHERE id=?',['姓名','"图图片地址"',2])
  db.query('INSERT INTO info (name,url) VALUES (?,?)',['小红','添加地址'],(err,resulte)=>{
    console.log('插入数据响应结果 ===> ',resulte)
    if(resulte.affectedRows == 1){
      console.log("成功")
      db.query('SELECT * FROM info',(err,resulte)=>{
        console.log("打印数据 ===> ",resulte)
      })
    }
  })
  res.send("插入数据...")
})

app.get("/del",(req,res)=>{
  db.query("DELETE FROM info  WHERE id=?",5,(err,resulte)=>{
    if(resulte.affectedRows == 1){
      console.log("删除成功 ===> ",resulte)

    }
    db.query('SELECT * FROM info',(err,resulte)=>{
      console.log("打印数据 ===> ",resulte)
    })
  })

  res.send("删除数据")
})



app.listen(3561,()=>{
  console.log("runing server http://localhost:3561")
})